// Local
#include "testhop.h"
#include "hop.h"
#include "weight.h"

// QtCore
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QString>

// QtXml
#include <QDomDocument>

// QtTest
#include <QTest>

// Entry point
QTEST_MAIN(testHop)

void testHop::Basics()
{
    QString Name = "Brewer's Gold";
    int Version = 981729;
    float Alpha = 7.4f;
    Weight Amount(5,Pound);
    HopUse iUse = HUDryHop;
    Duration Time(2573,Second);
    Hop zeHop(Name,Version,Alpha,Amount,iUse,Time);

    QVERIFY(zeHop._Name==Name);
    QVERIFY(zeHop._Version==Version);
    QVERIFY(zeHop._Alpha==Alpha);
    QVERIFY(zeHop._Amount==Amount);
    QVERIFY(zeHop._Use==iUse);
    QVERIFY(zeHop._Time==Time);
}

void testHop::ParseXML()
{
    Q_INIT_RESOURCE(test_hop);

    // Parse XML
    QFile Input(":/cascade.xml");
    QDomDocument doc("cascade");
    QVERIFY(Input.open(QIODevice::ReadOnly));
    QVERIFY(doc.setContent(&Input));

    Hop zeHop;
    QVERIFY(0==zeHop.CreateFromXMLNode(doc));
    QVERIFY(zeHop._Name=="Cascade");
    QVERIFY(zeHop._Version==1);
    QVERIFY(zeHop._Alpha==5.0);
    QVERIFY(zeHop._Amount==Weight(100,Gram));
    QVERIFY(zeHop._Use==HUBoil);
    QVERIFY(zeHop._Time==Duration(1,Hour));
}

void testHop::ToXML()
{
    Hop zeHop;
    zeHop._Name = "Name";
    zeHop._Version = 1;
    zeHop._Alpha = 6.0f;
    zeHop._Amount = Weight(65,Gram);
    zeHop._Use = HUDryHop;
    zeHop._Time = Duration(1,Hour);
    zeHop._Notes = "This is a short note. For testing purpose...";
    zeHop._Type = HTAroma;
    zeHop._Form = HFPellet;
    zeHop._Beta = 4.4;
    zeHop._HSI = 14.2;
    zeHop._Origin = "England";
    zeHop._Substitutes = "No substitues";
    zeHop._Humulene = 10.3;
    zeHop._Caryophyllene = 66.23;
    zeHop._Cohumulone = 9.9876;
    zeHop._Myrcene = 1.00023;

    QString zeString;
    QTextStream Stream(&zeString);
    QDomDocument docWrite("beerxml");
    zeHop.ToXMLNode(docWrite);
    docWrite.save(Stream,2);
    qDebug() << "Dumping generated XML" << endl << zeString;

    QDomDocument docRead("beerxml");
    QVERIFY(docRead.setContent(zeString));
    Hop parsedHop;
    QVERIFY(0==parsedHop.CreateFromXMLNode(docRead));
    QVERIFY(zeHop==parsedHop);
}
